package com.dzm.demo.myshop.web.admin.web.controller;

import com.dzm.demo.myshop.commons.dto.BaseResult;
import com.dzm.demo.myshop.domain.TbContent;
import com.dzm.demo.myshop.web.admin.abstracts.AbstractsBaseController;
import com.dzm.demo.myshop.web.admin.service.TbContentService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

@Controller
@RequestMapping(value = "content")
public class ContentController extends AbstractsBaseController<TbContent,TbContentService> {

    @Autowired
    private TbContentService tbContentService;

    @ModelAttribute
    public TbContent getTbUser(Long id){
        TbContent tbContent = null;
        //id不为空，则重数据库获取
        if (id != null){
            tbContent = tbContentService.getById(id);
        }else{
            tbContent = new TbContent();
        }
        return tbContent;
    }

    /**
     * 跳转到内容列表页
     * @return
     */
    @Override
    @RequestMapping(value = "list",method = RequestMethod.GET)
    public String list(){
        return "content_list";
    }

    /**
     * 跳转内容表单页
     * @return
     */
    @Override
    @RequestMapping(value = "form",method = RequestMethod.GET)
    public String form(){
        return "content_form";
    }

    @Override
    @RequestMapping(value = "detail",method = RequestMethod.GET)
    public String detail() {
        return "content_detail";
    }

    /**
     * 保存内容信息
     * @param tbContent
     * @param redirectAttributes
     * @return
     */
    @Override
    @RequestMapping(value = "save",method = RequestMethod.POST)
    public String save(TbContent tbContent, Model model, RedirectAttributes redirectAttributes){
        BaseResult baseResult = tbContentService.save(tbContent);

        //保存成功
        if (baseResult.getStatus() == 200){
            redirectAttributes.addFlashAttribute("baseResult",baseResult);
            return "redirect:/content/list";
        }

        //保存失败
        else {
            model.addAttribute("baseResult",baseResult);
            return "content_form";
        }
    }

    /**
     * 删除信息
     * @param ids
     * @return
     */
    @Override
    @ResponseBody
    @RequestMapping(value = "delete",method = RequestMethod.POST)
    public BaseResult delete(String ids){
        BaseResult baseResult = null;
        if (StringUtils.isNoneBlank(ids)){
            String[] idArray = ids.split(",");
            tbContentService.deleteMulti(idArray);
            baseResult = BaseResult.success("删除内容成功！");
        }else {
            baseResult = BaseResult.fail("删除内容失败，请稍后尝试！");
        }
        return baseResult;
    }


}
